﻿using FileSC.Configs;
using System;
using System.Collections.Generic;
using System.IO;

namespace FileSC.WEBService.Helper
{
    internal static class PathHelper
    {

        private static bool SubPathContains(this string dir, string path)
        {
            var fulldir = Path.GetFullPath(dir);
            var fullpath = Path.GetFullPath(path);
            if (fullpath.Length <= fulldir.Length) return false;
            if (fullpath.Substring(0, fulldir.Length).Equals(fulldir)) return true;
            return false;
        }
        /// <summary>
        /// 获取下载缓存路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        /// <remarks>
        /// 必须在存储路径内，如果在存储路径之外，或者path为空，则采用默认存储子路径
        /// </remarks>
        public static string GetDownloadCacheFile(string path)
        {
            var basePath = AppConfig.Text("存储路径");
            var now_path = basePath;
            path = Path.GetDirectoryName(path);
            if (! string.IsNullOrEmpty(path)) now_path = Path.Combine(basePath, path.TrimStart('\\'));
            if (! basePath.SubPathContains(now_path))
            {
                now_path = Path.Combine(basePath, AppConfig.Text("默认"));
            }
            Directory.CreateDirectory(now_path);

            return now_path;
        }
    }
}
